/*
 * Copyright (c) 2017-present, CV4J Contributors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.core.filters;

import ohos.agp.colors.RgbColor;

import com.cv4j.core.datamodel.ColorProcessor;
import com.cv4j.core.datamodel.ImageProcessor;
import com.cv4j.image.util.Tools;

import java.util.Optional;

/**
 * 装饰图案过滤器
 * Vignette - a photograph whose edges shade off gradually
 *
 * @author dev
 * @date 2021/07/22
 */
public class VignetteFilter extends BaseFilter {
    private int vignetteWidth;
    private int fade;
    private RgbColor vignetteColor;

    /**
     * 装饰图案过滤器
     */
    public VignetteFilter() {
        vignetteWidth = 50;
        fade = 35;
        vignetteColor = new RgbColor(0x00, 0x00, 0x00);
    }

    /**
     * 做的过滤器
     *
     * @param src src
     * @return {@link ImageProcessor}
     */
    @Override
    public Optional<ImageProcessor> doFilter(ImageProcessor src) {
        int total = width * height;
        byte[][] output = new byte[3][total];
        int index = 0;
        for (int row = 0; row < height; row++) {
            int ta = 0;
            int tr = 0;
            int tg = 0;
            int tb = 0;
            for (int col = 0; col < width; col++) {
                int dX = Math.min(col, width - col);
                int dY = Math.min(row, height - row);
                index = row * width + col;
                tr = RED[index] & 0xff;
                tg = GREED[index] & 0xff;
                tb = BLUE[index] & 0xff;
                if ((dY <= vignetteWidth) & (dX <= vignetteWidth)) {
                    double k1 = 1 - (double) (Math.min(dY, dX) - vignetteWidth + fade) / (double) fade;
                    int[] rgb = superpositionColor(tr, tg, tb, k1);
                    output[0][index] = (byte) rgb[0];
                    output[1][index] = (byte) rgb[1];
                    output[2][index] = (byte) rgb[2];
                    continue;
                }

                if ((dX < (vignetteWidth - fade)) | (dY < (vignetteWidth - fade))) {
                    RgbColor rgbColor = new RgbColor(vignetteColor);
                    output[0][index] = (byte) rgbColor.getRed();
                    output[1][index] = (byte) rgbColor.getGreen();
                    output[2][index] = (byte) rgbColor.getBlue();
                } else {
                    if ((dX < vignetteWidth) & (dY > vignetteWidth)) {
                        double k1 = 1 - (double) (dX - vignetteWidth + fade) / (double) fade;
                        int[] rgb = superpositionColor(tr, tg, tb, k1);
                        output[0][index] = (byte) rgb[0];
                        output[1][index] = (byte) rgb[1];
                        output[2][index] = (byte) rgb[2];
                    } else {
                        if ((dY < vignetteWidth) & (dX > vignetteWidth)) {
                            double k1 = 1 - (double) (dY - vignetteWidth + fade) / (double) fade;
                            int[] rgb = superpositionColor(tr, tg, tb, k1);
                            output[0][index] = (byte) rgb[0];
                            output[1][index] = (byte) rgb[1];
                            output[2][index] = (byte) rgb[2];
                        } else {
                            output[0][index] = (byte) tr;
                            output[1][index] = (byte) tg;
                            output[2][index] = (byte) tb;
                        }
                    }
                }
            }
        }

        ((ColorProcessor) src).putRGB(output[0], output[1], output[2]);
        output = null;
        return Optional.ofNullable(src);
    }

    /**
     * 叠加颜色
     *
     * @param red 红色的
     * @param green 绿色
     * @param blue 蓝色的
     * @param k1 k1
     * @return {@link int[]}
     */
    public int[] superpositionColor(int red, int green, int blue, double k1) {
        int r1 = red;
        int g1 = green;
        int b1 = blue;
        RgbColor rgbColor = new RgbColor(vignetteColor);
        r1 = (int) (rgbColor.getRed() * k1 + red * (1.0 - k1));
        g1 = (int) (rgbColor.getGreen() * k1 + green * (1.0 - k1));
        b1 = (int) (rgbColor.getBlue() * k1 + blue * (1.0 - k1));
        return new int[]{Tools.clamp(r1), Tools.clamp(g1), Tools.clamp(b1)};
    }

    /**
     * 得到小插图宽度
     *
     * @return int
     */
    public int getVignetteWidth() {
        return vignetteWidth;
    }

    /**
     * 设置小插图宽度
     *
     * @param vignetteWidth 装饰图案的宽度
     */
    public void setVignetteWidth(int vignetteWidth) {
        this.vignetteWidth = vignetteWidth;
    }

    /**
     * 会褪色
     *
     * @return int
     */
    public int getFade() {
        return fade;
    }

    /**
     * 设置褪色
     *
     * @param fade 褪色
     */
    public void setFade(int fade) {
        this.fade = fade;
    }

    /**
     * 得到装饰图案的颜色
     *
     * @return int
     */
    public int getVignetteColor() {
        return vignetteColor.asArgbInt();
    }

    /**
     * 设置装饰图案颜色
     *
     * @param vignetteColor 装饰图案的颜色
     */
    public void setVignetteColor(int vignetteColor) {
        this.vignetteColor = new RgbColor(vignetteColor);
    }
}
